package com.AAL.Action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.LogForPlanDAO;
import com.AAL.DAO.PlanDAO;
import com.AAL.DAO.StatusOfPlanDAO;
import com.AAL.DAO.TypeOfAutoDAO;
import com.AAL.DAO.TypesOfWheelDAO;
import com.AAL.DAO.WarehouseDAO;
import com.AAL.DAO.WheelDetailDAO;
import com.AAL.Model.Account;
import com.AAL.Model.LogForPlan;
import com.AAL.Model.Plan;
import com.AAL.Model.StatusOfPlan;
import com.AAL.Model.TypeOfAuto;
import com.AAL.Model.TypesOfWheel;
import com.AAL.Model.WareHouse;
import com.AAL.Model.WheelDetail;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class PlanAction extends ActionSupport implements SessionAware {

	private Account myAccount;

	private Plan plan;
	private WareHouse wareHouse;
	private TypeOfAuto typeOfAuto;
	private TypesOfWheel typesOfWheel;
	private LogForPlan logForPlan;
	private List<StatusOfPlan> lstStatusOfPlans;
	private List<TypeOfAuto> lstTypeOfAutos;
	private List<TypesOfWheel> lstTypesOfWheels;
	private List<WheelDetail> lstWheelDetails;
	private List<WareHouse> lstWareHouses;

	private TypeOfAutoDAO lnkTypeOfAutoDAO;
	private TypesOfWheelDAO lnkTypesOfWheelDAO;
	private WheelDetailDAO lnkWheelDetailDAO;
	private PlanDAO lnkPlanDAO;
	private LogForPlanDAO lnkLogForPlanDAO;
	private StatusOfPlanDAO lnkStatusOfPlanDAO;
	private WarehouseDAO lnkWarehouseDAO;

	private Map<String, Object> session;

	public PlanAction() {
		this.lnkLogForPlanDAO = new LogForPlanDAO();
		this.lnkPlanDAO = new PlanDAO();
		this.lnkStatusOfPlanDAO = new StatusOfPlanDAO();
		this.lnkTypeOfAutoDAO = new TypeOfAutoDAO();
		this.lnkTypesOfWheelDAO = new TypesOfWheelDAO();
		this.lnkWarehouseDAO = new WarehouseDAO();
		this.lnkWheelDetailDAO = new WheelDetailDAO();
	}

	// quan ly ds ke hoach
	public String planManagement() {
		try {
			System.out.println("Goi ham plan management");

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// them moi mot ke hoach
	public String addPlan() {
		try {
			System.out.println("Goi ham add plan");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String addPlan_step1() {
		try {
			System.out.println("Add plan - step 1");
			// luu thong tin chung
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// luu thong tin danh sach chi tiet ke hoach : gom nhung lop xe nao, so
	// luong la bao nhiu
	public String addPlan_step2_getWarehouse() {
		try {
			System.out.println("Add plan - step 2 -  Warehouse");
			// Thong tin ve cac nha kho
			// nho kiem tra session truoc khi xoa, kiem tra session nao that can
			// thiet thi luu giu va tranh de truy van long
			 session.clear();
			List<WareHouse> lstWareHousesTemp = lnkWarehouseDAO
					.getLstWareHouses();
			this.lstWareHouses = new ArrayList<WareHouse>();
			for (WareHouse item : lstWareHousesTemp) {
				WareHouse a = new WareHouse();
				a.setWareHouseID(item.getWareHouseID());
				a.setWareHouseName(item.getWareHouseName());
				this.lstWareHouses.add(a);
			}
			System.out.println("Warehouse:" + this.lstWareHouses);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String addPlan_step2_getTypeOfAutos() {
		try {
			System.out
					.println("Add plan - step 2 - getTypeOfAutos ");
			// Thong tin ve nhom dong xe
			List<TypeOfAuto> lstTypeOfAutosTemp = lnkTypeOfAutoDAO
					.getAllTypeOfAutos();
			this.lstTypeOfAutos = new ArrayList<TypeOfAuto>();
			for (TypeOfAuto item : lstTypeOfAutosTemp) {
				TypeOfAuto a = new TypeOfAuto();
				a.setTypeOfAutoID(item.getTypeOfAutoID());
				a.setTypeOfAutoName(item.getTypeOfAutoName());
				this.lstTypeOfAutos.add(a);
			}
			System.out.println("LstTypeOfAutos:" + this.lstTypeOfAutos);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String addPlan_step2_getTypeOfWheel() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
		.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			System.out.println("Add plan - step 2 - getTypeOfWheels");
			TypeOfAuto typeOfAutoTemp = lnkTypeOfAutoDAO.getTypeOfAuto(request.getParameter("typeOfAutos"));
			List<TypesOfWheel> lstTypesOfWheelsTempl = lnkTypesOfWheelDAO
					.getLstTypesOfWheelsByTypeOfAutos(typeOfAutoTemp);
			this.lstTypesOfWheels = new ArrayList<TypesOfWheel>();
			for (TypesOfWheel item : lstTypesOfWheelsTempl) {
				TypesOfWheel a = new TypesOfWheel();
				a.setTypeOfWheelID(item.getTypeOfWheelID());
				a.setProductName(item.getProductName());
				this.lstTypesOfWheels.add(a);
			}
			System.out.println("TypeOfWheel:" + this.lstTypesOfWheels);
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String addPlan_step2_getWheelDetails() {
		try {
			System.out.println("Add plan - step 2 - getWheelDetails");
			this.lstWheelDetails = lnkWheelDetailDAO
					.getLstWheelDetailsByTypeOfWheel(this.typesOfWheel);

		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	public String addPlan_step2_saveLstWheelDetail() {
		try {
			System.out.println("Add plan - step 2 - save LstWheelDetails");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	// them moi mot ke hoach
	public String addPlanReport() {
		try {
			System.out.println("Goi ham add plan report");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public Account getMyAccount() {
		return myAccount;
	}

	public void setMyAccount(Account myAccount) {
		this.myAccount = myAccount;
	}

	public Plan getPlan() {
		return plan;
	}

	public void setPlan(Plan plan) {
		this.plan = plan;
	}

	public LogForPlan getLogForPlan() {
		return logForPlan;
	}

	public void setLogForPlan(LogForPlan logForPlan) {
		this.logForPlan = logForPlan;
	}

	public List<StatusOfPlan> getLstStatusOfPlans() {
		return lstStatusOfPlans;
	}

	public void setLstStatusOfPlans(List<StatusOfPlan> lstStatusOfPlans) {
		this.lstStatusOfPlans = lstStatusOfPlans;
	}

	public List<TypeOfAuto> getLstTypeOfAutos() {
		return lstTypeOfAutos;
	}

	public void setLstTypeOfAutos(List<TypeOfAuto> lstTypeOfAutos) {
		this.lstTypeOfAutos = lstTypeOfAutos;
	}

	public List<TypesOfWheel> getLstTypesOfWheels() {
		return lstTypesOfWheels;
	}

	public void setLstTypesOfWheels(List<TypesOfWheel> lstTypesOfWheels) {
		this.lstTypesOfWheels = lstTypesOfWheels;
	}

	public List<WheelDetail> getLstWheelDetails() {
		return lstWheelDetails;
	}

	public void setLstWheelDetails(List<WheelDetail> lstWheelDetails) {
		this.lstWheelDetails = lstWheelDetails;
	}

	public List<WareHouse> getLstWareHouses() {
		return lstWareHouses;
	}

	public void setLstWareHouses(List<WareHouse> lstWareHouses) {
		this.lstWareHouses = lstWareHouses;
	}

	public TypeOfAuto getTypeOfAuto() {
		return typeOfAuto;
	}

	public void setTypeOfAuto(TypeOfAuto typeOfAuto) {
		this.typeOfAuto = typeOfAuto;
	}

	public TypesOfWheel getTypesOfWheel() {
		return typesOfWheel;
	}

	public void setTypesOfWheel(TypesOfWheel typesOfWheel) {
		this.typesOfWheel = typesOfWheel;
	}

	public WareHouse getWareHouse() {
		return wareHouse;
	}

	public void setWareHouse(WareHouse wareHouse) {
		this.wareHouse = wareHouse;
	}

}
